package net.hands.ado.db.jdbc.dialect;

import java.sql.Types;

public class MySQLDialect extends Dialect {

	private final SQLFunction FUNCTION = new MySQLFunction();

	public MySQLDialect() {
		super();
		registerColumnType(Types.BIT, "bit");
		registerColumnType(Types.BIGINT, "bigint");
		registerColumnType(Types.SMALLINT, "smallint");
		registerColumnType(Types.TINYINT, "tinyint");
		registerColumnType(Types.INTEGER, "integer");
		registerColumnType(Types.CHAR, "char(1)");
		registerColumnType(Types.FLOAT, "float");
		registerColumnType(Types.DOUBLE, "double precision");
		registerColumnType(Types.DATE, "date");
		registerColumnType(Types.TIME, "time");
		registerColumnType(Types.TIMESTAMP, "datetime");
		registerColumnType(Types.VARBINARY, "longblob");
		registerColumnType(Types.VARBINARY, 16777215, "mediumblob");
		registerColumnType(Types.VARBINARY, 65535, "blob");
		registerColumnType(Types.VARBINARY, 255, "tinyblob");
		registerColumnType(Types.BINARY, "binary($l)");
		registerColumnType(Types.LONGVARBINARY, "longblob");
		registerColumnType(Types.LONGVARBINARY, 16777215, "mediumblob");
		registerColumnType(Types.NUMERIC, "decimal($p,$s)");
		registerColumnType(Types.BLOB, "longblob");
		// registerColumnType( Types.BLOB, 16777215, "mediumblob" );
		// registerColumnType( Types.BLOB, 65535, "blob" );
		registerColumnType(Types.CLOB, "longtext");
		// registerColumnType( Types.CLOB, 16777215, "mediumtext" );
		// registerColumnType( Types.CLOB, 65535, "text" );

		registerVarcharTypes();
	}

	protected void registerVarcharTypes() {
		registerColumnType(Types.VARCHAR, "longtext");
		// registerColumnType( Types.VARCHAR, 16777215, "mediumtext" );
		registerColumnType(Types.VARCHAR, 65535, "varchar($l)");
		registerColumnType(Types.LONGVARCHAR, "longtext");
	}

	/**
	 * drop语句 支持if exists 在表名 前
	 */
	@Override
	public boolean supportsIfExistsBeforeTableName() {

		return true;
	}

	@Override
	public String getAddColumnString(String tableName, String columnName) {

		return "alter table " + tableName + " add column " + columnName + " ";
	}

	@Override
	public String getDropColumnString(String tableName, String columnName) {

		return "alter table " + tableName + " drop column " + columnName + " ";
	}

	@Override
	public String dropUniquesOnAlter(String tableName, String uk) {

		return new StringBuilder("alter table ").append(tableName)
				.append(" drop index ").append(uk).toString();
	}

	@Override
	public String dropIndex(String tableName, String indexkey) {

		return "drop index " + indexkey + " on " + tableName;
	}

	@Override
	public String getTableComment(String comment) {

		return " comment '" + comment + "'";
	}

	@Override
	public String getColumnComment(String comment) {

		return " comment '" + comment + "'";
	}

	@Override
	public SQLFunction getFunction() {

		return FUNCTION;
	}

	@Override
	public String toLimitSQL(final String sql, final int i, final int fetchSize) {
		final StringBuilder sb = new StringBuilder();
		sb.append(sql).append(" limit ");
		sb.append(i).append(",").append(fetchSize);
		return sb.toString();
	}
}
